package cn.itsource.hrm.web.controller;

import cn.itsource.hrm.dto.CourseDto;
import cn.itsource.hrm.service.ICourseService;
import cn.itsource.hrm.domain.Course;
import cn.itsource.hrm.query.CourseQuery;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;

    /**
     * 保存和修改公用的
     *   传递的实体
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@Valid @RequestBody CourseDto courseDto){
            courseService.insert(courseDto);
            return AjaxResult.me();

    }

    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return AjaxResult.me();

    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Course get(@PathVariable("id")Long id)
    {
        return courseService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Course> list(){

        return courseService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Course> json(@RequestBody CourseQuery query)
    {
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        page = courseService.selectPage(page);
        return new PageList<Course>(page.getTotal(),page.getRecords());
    }

    /**
     *
     * 课程上线
     * @return
     */
    @RequestMapping(value = "/onLineCourse/{id}",method = RequestMethod.POST)
    public AjaxResult onLineCourse(@PathVariable("id") Long id){
        courseService.onLineCourse(id);
        return AjaxResult.me();
    }

    /**
     *
     * 课程下线
     * @return
     */
    @RequestMapping(value = "/offLineCourse/{id}",method = RequestMethod.POST)
    public AjaxResult offLineCourse(@PathVariable("id") Long id){
        courseService.offLineCourse(id);
        return AjaxResult.me();
    }
}
